package cn.fl.ref.basicarchivesinfo.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.basicarchivesinfo.service.IBasicArchivesInfoService;
import cn.fl.ref.basicarchivesinfo.transform.BasicArchivesInfoVOTransform;
import cn.fl.ref.basicarchivesinfo.vo.BasicArchivesInfoVO;
import fl.ref.basicarchives.condition.BasicArchivesInfoQueryCondition;
import fl.ref.basicarchives.dto.BasicArchivesInfoDTO;

/**
 * @Description: 基础档案资料管理
 * @author mrzhang
 *
 */

@Controller
@RequestMapping("/basic/archivesinfo")
public class BasicArchivesInfoController extends BaseController{

	private static final Logger logger = LoggerFactory.getLogger(BasicArchivesInfoController.class);
	
	@Autowired
	private IBasicArchivesInfoService basicArchivesInfoService;
	
	/**
	 * 跳转到基础档案资料管理列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response){
		logger.info("加载基础档案资料管理列表页面.");
		ModelAndView view = new ModelAndView("rfbase/archivesinfo/archivesInfoList");
		//基础档案资料信息
//		List<BasicArchivesInfoVO> archivesList = 
		
		return view;
	}
	
	/**
	 * 初始化列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String warrNm = request.getParameter("warrNm");
		String warrTypeCd = request.getParameter("warrTypeCd");
		String warrLv = request.getParameter("warrLv");
		
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		
		BasicArchivesInfoQueryCondition bc = new BasicArchivesInfoQueryCondition();
		if (StringUtils.isNotEmpty(warrNm)) {
			bc.setWarrNm(warrNm.trim());
		}
		if (StringUtils.isNotEmpty(warrTypeCd)) {
			bc.setWarrTypeCd(Integer.parseInt(warrTypeCd.trim()));
		}
		if (StringUtils.isNotEmpty(warrLv)) {
			bc.setWarrLv(Integer.parseInt(warrLv.trim()));
		}
		bc.setSysCd(CurrentThreadContext.getCurrentSysCd());
		bc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		page.setCondition(bc);
		
		Pager resultPager = this.basicArchivesInfoService.searchListPage(page);
		List<BasicArchivesInfoDTO> listDTO = (List<BasicArchivesInfoDTO>) resultPager.getResultList();
		List<BasicArchivesInfoVO> listVO = BasicArchivesInfoVOTransform.toVOList(listDTO);
		ListPageVO<BasicArchivesInfoVO> listPage = new ListPageVO<BasicArchivesInfoVO>(listVO,resultPager.getTotalCount());		
		
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(resultPager), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("rfbase/archivesinfo/archivesInfoAdd");
		String id =  request.getParameter("id");
		if(id != null && !("").equals(id)){
			logger.info("加载基础档案资料修改页面.");
			view.addObject("id",id);
			try {
				BasicArchivesInfoVO vo = this.basicArchivesInfoService.selectById(Long.valueOf(id), CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd());
				view.addObject("warrNm",vo.getWarrNm());
				view.addObject("warrTypeCd",vo.getWarrTypeCd());
				view.addObject("warrLv",vo.getWarrLv());
				view.addObject("custTypeCd",vo.getCustTypeCd());
				view.addObject("warStsCd",vo.getWarStsCd());
			} catch (BaseException ex) {
				BaseLogUtils.info(logger, "getById", "获取数据失败,主键[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
			} catch (Exception ex) {
				BaseLogUtils.error(logger, "getById", "获取数据失败,主键[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
			}
		}else{
			logger.info("加载基础档案资料添加页面.");
			view.addObject("id","");
			view.addObject("warrNm","");
			view.addObject("warrTypeCd","");
			view.addObject("warrLv","");
			view.addObject("custTypeCd","");
			view.addObject("warStsCd","");
		}
		return view;
	}
	
	/**
	 * 添加基础档案资料信息
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		logger.info("添加基础档案资料信息.");
		String warrNm = request.getParameter("warrNm").trim();				//权证名称
		String warrTypeCd = request.getParameter("warrTypeCd").trim();		//权证类别
		String warrLv = request.getParameter("warrLv").trim();				//权证级别
		String custTypeCd = request.getParameter("custTypeCd").trim();		//客户类型
		String warStsCd = request.getParameter("warStsCd").trim();			//婚姻状况
		
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		
		try {
			//验证参数
			if(StringUtils.isEmpty(warrNm)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"权证名称不能为空!");
			}
			if(StringUtils.isEmpty(warrTypeCd)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"权证类别不能为空!");
			}
			if(StringUtils.isEmpty(warrLv)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"权证级别不能为空!");
			}
			if(StringUtils.isEmpty(custTypeCd)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"客户类型不能为空!");
			}
			if(StringUtils.isEmpty(warStsCd)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"婚姻状况不能为空!");
			}
			
			BasicArchivesInfoVO vo = new BasicArchivesInfoVO();
			vo.setWarrNm(warrNm);
			vo.setWarrTypeCd(Integer.parseInt(warrTypeCd));
			vo.setWarrLv(Integer.parseInt(warrLv));
			vo.setCustTypeCd(Integer.parseInt(custTypeCd));
			vo.setWarStsCd(Integer.parseInt(warStsCd));
			vo.setAttCd("");
			vo.setIsDel(BaseConstant.IS_YESNO_NO);
			vo.setIsValid(BaseConstant.IS_YESNO_YES);
			vo.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
			vo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			vo.setCrtTm(new Date());
			vo.setMdfTm(new Date());
			
			long mainId = this.basicArchivesInfoService.add(vo);
			
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("新增成功!");
			BaseLogUtils.info(logger, "add", "新增数据 ,name[" + warrNm + "]. ID:" + mainId, CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败,name[" + warrNm + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("新增失败!");
			BaseLogUtils.error(logger, "add", "新增数据失败,name[" + warrNm + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		
		return result;
	}
	
	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET })
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("rfbase/archivesinfo/archivesInfoDetail");
		String id = request.getParameter("id");
		view.addObject("id", id);
		return view;
	}
	
	/**
	 * 根据id查询数据信息
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id").trim();
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "获取成功!");
		BasicArchivesInfoVO vo = null;
		try {
			vo = this.basicArchivesInfoService.selectById(Long.valueOf(id), CurrentThreadContext.getCurrentSysCd(),
					CurrentThreadContext.getCurrentOrgCd());
			result.setData(vo);
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取数据失败,主键[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "getById", "获取数据失败,主键[" + id + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 修改
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String id = request.getParameter("id").trim();
			String warrNm = request.getParameter("warrNm").trim();
			String warrTypeCd = request.getParameter("warrTypeCd").trim();
			String warrLv = request.getParameter("warrLv").trim();
			String custTypeCd = request.getParameter("custTypeCd").trim();
			String warStsCd = request.getParameter("warStsCd").trim();
			
			DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "编辑成功!");
		
			try {
			//验证参数
			if(StringUtils.isEmpty(warrNm)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"权证名称不能为空!");
			}
			if(StringUtils.isEmpty(warrTypeCd)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"权证类别不能为空!");
			}
			if(StringUtils.isEmpty(warrLv)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"权证级别不能为空!");
			}
			if(StringUtils.isEmpty(custTypeCd)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"客户类型不能为空!");
			}
			if(StringUtils.isEmpty(warStsCd)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"婚姻状况不能为空!");
			}
			
			BasicArchivesInfoVO vo = new BasicArchivesInfoVO();
			vo.setId(Long.valueOf(id));
			vo.setWarrNm(warrNm);
			vo.setWarrTypeCd(Integer.parseInt(warrTypeCd));
			vo.setWarrLv(Integer.parseInt(warrLv));
			vo.setCustTypeCd(Integer.parseInt(custTypeCd));
			vo.setWarStsCd(Integer.parseInt(warStsCd));
			vo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			vo.setMdfTm(new Date());
			
			this.basicArchivesInfoService.modify(vo);
			
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("编辑成功!");
			BaseLogUtils.info(logger, "modify", "编辑数据 ,name[" + warrNm + "]. ID:" + id, CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "编辑数据失败,name[" + warrNm + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("编辑失败!");
			BaseLogUtils.error(logger, "modify", "编辑数据失败,name[" + warrNm + "]." + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		
		return result;
	}
	
	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/remove.do", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public DataResultVO remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		try {
			BasicArchivesInfoVO vo = new BasicArchivesInfoVO();
			vo.setId(Long.valueOf(id));
			vo.setIsValid(BaseConstant.IS_YESNO_NO);
			vo.setIsDel(BaseConstant.IS_YESNO_YES);
			vo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
			vo.setMdfTm(new Date());
			
			this.basicArchivesInfoService.modify(vo);
			result.setSuccess(BaseConstant.IS_YESNO_YES);
			result.setInfo("删除成功!");
			BaseLogUtils.info(logger, "remove", "删除数据,主键["+id+"].", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "remove", "删除数据失败,主键["+id+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "remove", "删除数据失败,主键["+id+"]."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
}
